﻿
using System ;
using System.IO ;
using System.Windows.Forms ;

using MskEdit.Msk.Model ;


namespace MskEdit.Msk.Backend
{
    public static class MskDocumentIO 
    {
        
        public static string TryGetInputPath( string fallbackValue ) {
        
            var openFileDialog = new OpenFileDialog {
                DefaultExt       = "msk",
                Filter           = "MSK Files|*.msk",
                InitialDirectory = "", // MskSession.InputDirectory = "",
                RestoreDirectory = true,
                Title            = "Open input MSK File"
            } ;
            
            openFileDialog.ShowDialog() ;

            if ( string.IsNullOrEmpty( openFileDialog.FileName ) )
                return fallbackValue ;
                
            return openFileDialog.FileName ;
        }


        public static string TryGetOutputPath( string fallbackValue ) {
        
            var saveFileDialog = new SaveFileDialog {
                DefaultExt       = "msk",
                Filter           = "MSK Files|*.msk",
                InitialDirectory = "", // MskSession.OutputDirectory = "",
                RestoreDirectory = true,
                Title            = "Open output MSK File"
            } ;
            
            saveFileDialog.ShowDialog() ;

            if ( string.IsNullOrEmpty( saveFileDialog.FileName ) )
                return fallbackValue ;
                
            return saveFileDialog.FileName ;
        }
        
        
        public static MskDocument TryRead( FileInfo mskFileInfo, MskDocument fallbackValue ) {
        
            string[] allLines = File.ReadAllLines( mskFileInfo.FullName ) ; 
            
            if ( ( allLines == null ) || ( allLines.Length == 0 ) )
                return fallbackValue ;       
                
            MskDocument mskDocument = new MskDocument( mskFileInfo.Name, allLines ) ;
            
            return mskDocument ;
        }
        
        
        public static void TryWrite( String fullPath, MskDocument doc ) {
        
            if ( doc.AllLines.Length == 0 )
                return ;
        
            File.WriteAllLines( fullPath, doc.AllLines ) ; 
        }
        
        
        public static string TryGetMskFileName( string projectDirectoryName, string fallbackValue ) {
        
            DirectoryInfo projectDirectoryInfo = new DirectoryInfo( projectDirectoryName ) ;
            
            FileInfo[]  mskFileInfos = projectDirectoryInfo.GetFiles( "*.msk" ) ;
            
            if ( ( mskFileInfos == null ) || ( mskFileInfos.Length == 0 ) )
                return fallbackValue ;
                
            return mskFileInfos[0].FullName ;
        }
        
        
        public static MskDocument TryGetMskDocument( DirectoryInfo projectDirectoryInfo, MskDocument fallbackValue ) {
        
            if ( projectDirectoryInfo.Exists == false )
                return fallbackValue ;

            FileInfo[]  mskFileInfos = projectDirectoryInfo.GetFiles( "*.msk" ) ;
            
            if ( ( mskFileInfos == null ) || ( mskFileInfos.Length == 0 ) )
                return fallbackValue ;
                
            return TryRead( mskFileInfos[0], fallbackValue ) ;
        }
        
        
    }
}
